<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>find</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href="find.html">find</a>
  </div>

  <div class="name-format">
    find
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  iterator find( iterator start, iterator end, const <a href=
"../containers.html">TYPE</a>&amp; val );
</pre>

  <p>The find() algorithm looks for an element matching <em>val</em>
  between <em>start</em> and <em>end</em>. If an element matching
  <em>val</em> is found, the return value is an iterator that points to
  that element. Otherwise, the return value is an iterator that points
  to <em>end</em>.</p>

  <p>For example, the following code uses find() to search a vector of
  integers for the number 3:</p>
  <pre class="example-code">
 int num_to_find = 3;           

 vector&lt;int&gt; v1;
 for( int i = 0; i &lt; 10; i++ ) {
   v1.push_back(i);
 }              

 vector&lt;int&gt;::iterator result;
 result = find( v1.begin(), v1.end(), num_to_find );            

 if( result == v1.end() ) {
   cout &lt;&lt; &quot;Did not find any element matching &quot; &lt;&lt; num_to_find &lt;&lt; endl;
 }              

 else {
   cout &lt;&lt; &quot;Found a matching element: &quot; &lt;&lt; *result &lt;&lt; endl;
 }              
</pre>

  <p>In the next example, shown below, the find() function is used on
  an array of integers. This example shows how the C++ Algorithms can
  be used to manipulate arrays and pointers in the same manner that
  they manipulate containers and iterators:</p>
  <pre class="example-code">
 int nums[] = { 3, 1, 4, 1, 5, 9 };

 int num_to_find = 5;
 int start = 0;
 int end = 2;
 int* result = find( nums + start, nums + end, num_to_find );                

 if( result == nums + end ) {
   cout &lt;&lt; &quot;Did not find any number matching &quot; &lt;&lt; num_to_find &lt;&lt; endl;
 } else {
   cout &lt;&lt; &quot;Found a matching number: &quot; &lt;&lt; *result &lt;&lt; endl;
 }              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="adjacent_find.html">adjacent_find</a><br>
    <a href="find_end.html">find_end</a><br>
    <a href="find_first_of.html">find_first_of</a><br>
    <a href="find_if.html">find_if</a><br>
    <a href="mismatch.html">mismatch</a><br>
    <a href="search.html">search</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
